package com.java.study;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Component;

/**
 * @author xuyuansheng
 */
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "testGroup", topic = "testTopic")
public class MessageListener implements RocketMQListener<MessageExt>, RocketMQPushConsumerLifecycleListener {
    @Override
    public void onMessage(MessageExt msg) {
        String message = new String(msg.getBody());
        String brokerName = msg.getBrokerName();
        String topic = msg.getTopic();
        long queueOffset = msg.getQueueOffset();
        log.info("Received message: {} from broker: {} topic: {} queueOffset: {}", message, brokerName, topic, queueOffset);

    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        log.info("Prepare to start consumer: {}", consumer.getConsumerGroup());
    }
}
